package com.ruoyi.swla2.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.swla2.domain.WarnHistory;
import com.ruoyi.swla2.service.IWarnHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 告警历史Controller
 *
 * @author gdqt
 * @date 2023-09-11
 */
@RestController
@RequestMapping("/swla2/warnHis")
public class WarnHistoryController extends BaseController {
    @Autowired
    private IWarnHistoryService warnHistoryService;

    /**
     * 查询告警列表
     */
    @PreAuthorize("@ss.hasPermi('params:strategyParams:list')")
    @GetMapping("/list")
    public TableDataInfo list(WarnHistory warn) {
        startPage();
        List<WarnHistory> list = warnHistoryService.selectWarnList(warn);
        return getDataTable(list);
    }

    /**
     * 导出告警列表
     */
    @PreAuthorize("@ss.hasPermi('params:strategyParams:export')")
    @Log(title = "告警", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WarnHistory warn) {
        List<WarnHistory> list = warnHistoryService.selectWarnList(warn);
        ExcelUtil<WarnHistory> util = new ExcelUtil<WarnHistory>(WarnHistory.class);
        util.exportExcel(response, list, "告警数据");
    }

    /**
     * 获取告警详细信息
     */
    @PreAuthorize("@ss.hasPermi('params:strategyParams:query')")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        return success(warnHistoryService.selectWarnByID(ID));
    }

    /**
     * 新增告警
     */
    @PreAuthorize("@ss.hasPermi('params:strategyParams:add')")
    @Log(title = "告警", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WarnHistory warn) {
        warn.setTenantId(this.getTenantId());
        return toAjax(warnHistoryService.insertWarn(warn));
    }

    /**
     * 修改告警
     */
    @PreAuthorize("@ss.hasPermi('params:strategyParams:edit')")
    @Log(title = "告警", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WarnHistory warn) {
        return toAjax(warnHistoryService.updateWarn(warn));
    }

    /**
     * 删除告警
     */
    @PreAuthorize("@ss.hasPermi('params:strategyParams:remove')")
    @Log(title = "告警", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs) {
        return toAjax(warnHistoryService.deleteWarnByIDs(IDs));
    }
}
